# vim: set ft=make :
NAME := "tembo-pod-init"
#VERSION := `git rev-parse HEAD`
CERT_MANAGER_VERSION := "v1.12.3"
SEMVER_VERSION := `grep version Cargo.toml | awk -F"\"" '{print $2}' | head -n 1`
NAMESPACE := "default"
KUBE_VERSION := "1.26"
RUST_LOG := "debug"
ENV := "development"

default:
  @just --list --unsorted --color=always | rg -v "    default"

# install cert-manager
cert-manager:
  kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/{{CERT_MANAGER_VERSION}}/cert-manager.yaml
  sleep 7
  kubectl wait pods --for=condition=Ready --timeout=300s --all --all-namespaces

install-reloader:
	kubectl create namespace reloader || true
	helm upgrade --install --namespace=reloader reloader stakater/reloader --values ./testdata/reloader.yaml

install-kube-prometheus-stack:
	kubectl create namespace monitoring || true
	helm upgrade --install --namespace=monitoring monitoring prometheus-community/kube-prometheus-stack


install-tembo-pod-init:
	kubectl create namespace tembo-pod-init || true
	helm upgrade --install --namespace=tembo-pod-init --values=./testdata/pod-init.yaml tembo-pod-init ../charts/tembo-pod-init

install-cnpg:
	helm upgrade --install cnpg \
	 --namespace cnpg-system \
	 --create-namespace \
		--values=./testdata/cnpg.yaml \
	 cnpg/cloudnative-pg

install-tempo:
	helm upgrade --install \
		tempo grafana/tempo \
	 --namespace monitoring

enable-cnpg-default-namespace:
	kubectl label namespace default "tembo-pod-init.tembo.io/watch"="true"
	kubectl delete pods -n tembo-pod-init --all

update-helm-repos:
	helm repo add cnpg https://cloudnative-pg.github.io/charts
	helm repo add jetstack https://charts.jetstack.io
	helm repo add traefik https://traefik.github.io/charts
	helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
	helm repo add grafana https://grafana.github.io/helm-charts
	helm repo add stakater https://stakater.github.io/stakater-charts
	helm repo update

install-operator:
  kubectl apply -f ../charts/tembo-operator/templates/crd.yaml
  helm upgrade --install --create-namespace --namespace=coredb-operator --values=./testdata/operator-values.yaml tembo-operator ../charts/tembo-operator 

# delete kind
delete-kind:
	kind delete cluster && sleep 5

# start kind
start-kind:
	kind delete cluster || true
	scripts/start-kind-with-registry.sh
	just update-helm-repos
	just cert-manager
	just install-reloader
	just install-kube-prometheus-stack
	just install-tempo
	just install-cnpg
	just enable-cnpg-default-namespace
	kubectl wait pods --for=condition=Ready --timeout=300s --all --all-namespaces

# run
run:
  RUST_LOG={{RUST_LOG}} ENV={{ENV}} cargo run

# run cargo watch
watch:
  RUST_LOG={{RUST_LOG}} ENV={{ENV}} cargo watch -x 'run'

# format with nightly rustfmt
fmt:
  cargo +nightly fmt

# run integration testing
test:
  RUST_LOG={{RUST_LOG}} ENV={{ENV}} cargo test -- --ignored --nocapture
